package drone.basis.util ;

import java.io.File ;
import java.io.FileInputStream ;
import java.io.FileOutputStream ;

import org.apache.commons.io.IOUtils ;

public class HexUtil
{

	/**
	 * 字节转换为十六进制字符串
	 */
	public static String byte2Hex (
			byte source
	)
	{

		String hex = Integer.toHexString ( source & 0xFF ) ;
		if ( hex.length ( ) < 2 )
		{
			hex = "0" + hex ;
		}
		return hex.toUpperCase ( ) ;
	}

	/**
	 * 字节数组转16进制
	 */
	public static String bytes2Hex (
			byte [ ] source
	)
	{
		StringBuffer sb = new StringBuffer ( ) ;
		for ( int i = 0 ; i < source.length ; i ++ )
		{
			String hex = Integer.toHexString ( source [ i ] & 0xFF ) ;
			if ( hex.length ( ) < 2 )
			{
				sb.append ( 0 ) ;
			}
			sb.append ( hex ) ;
		}
		return sb.toString ( ).toUpperCase ( ) ;
	}

	/**
	 * int转hex
	 */
	public static String int2Hex (
			int source
	)
	{
		return String.format ( "%02x" , source ).toUpperCase ( ) ;
	}

	/**
	 * short转hex
	 */
	public static String short2Hex (
			short source
	)
	{
		return String.format ( "%02x" , source ).toUpperCase ( ) ;
	}

	/**
	 * 十六进制字符串转换为字节
	 */
	public static byte hex2Byte (
			String source
	)
	{
		return ( byte ) Integer.parseInt ( source , 16 ) ;
	}

	/**
	 * hex字符串转byte数组
	 */
	public static byte [ ] hex2Bytes (
			String source
	)
	{
		if ( StringUtil.isEmpty ( source ) )
		{
			return null ;
		}
		source = source.replaceAll ( " " , "" ) ;

		int hexlen = source.length ( ) ;
		byte [ ] result = new byte [ ( hexlen / 2 ) ] ;
		int j = 0 ;
		for ( int i = 0 ; i < hexlen ; i += 2 )
		{
			result [ j ] = hex2Byte ( source.substring ( i , i + 2 ) ) ;
			j ++ ;
		}
		return result ;
	}

	/**
	 * 十六进制字符串转换为int
	 */
	public static int hex2Int (
			String source
	)
	{
		return Integer.parseInt ( source , 16 ) ;
	}

	/**
	 * 十六进制字符串转换为short
	 */
	public static short hex2Short (
			String source
	)
	{
		return Short.parseShort ( source , 16 ) ;
	}

	/**
	 * Int转成最低位一个字节
	 */
	public static byte int2Byte (
			int source
	)
	{
		return ( byte ) ( source & 0xFF ) ;
	}

	/**
	 * Int转成4位字节数组
	 */
	public static byte [ ] int2Bytes (
			int source
	)
	{
		byte [ ] result = new byte [ 4 ] ;
		result [ 0 ] = ( byte ) ( ( source >> 24 ) & 0xFF ) ;
		result [ 1 ] = ( byte ) ( ( source >> 16 ) & 0xFF ) ;
		result [ 2 ] = ( byte ) ( ( source >> 8 ) & 0xFF ) ;
		result [ 3 ] = ( byte ) ( source & 0xFF ) ;
		return result ;
	}

	/**
	 * byte转Int
	 */
	public static int byte2Int (
			byte source
	)
	{
		return source & 0xff ;
	}

	/**
	 * 4位字节数组转Int
	 */
	public static int bytes2Int (
			byte [ ] source
	)
	{
		int value = 0 ;
		// 由高位到低位
		for ( int i = 0 ; i < 4 ; i ++ )
		{
			int shift = ( 4 - 1 - i ) * 8 ;
			value += ( source [ i ] & 0x000000FF ) << shift ;
		}
		return value ;
	}

	/**
	 * short转成一个byte
	 */
	public static byte short2Byte (
			short source
	)
	{
		return ( byte ) ( source & 0xFF ) ;
	}

	/**
	 * short转成2位字节数组
	 */
	public static byte [ ] short2Bytes (
			short source
	)
	{
		byte [ ] b = new byte [ 2 ] ;
		for ( int i = 0 ; i < 2 ; i ++ )
		{
			int offset = 16 - ( i + 1 ) * 8 ;
			b [ i ] = ( byte ) ( ( source >> offset ) & 0xff ) ;
		}
		return b ;
	}

	/**
	 * 一个字节转成short
	 */
	public static short byte2Short (
			byte source
	)
	{
		return ( short ) source ;
	}

	/**
	 * 2位字节数组转成short
	 * 
	 */
	public static short bytes2Short (
			byte [ ] source
	)
	{
		short l = 0 ;
		for ( int i = 0 ; i < 2 ; i ++ )
		{
			l <<= 8 ;
			l |= ( source [ i ] & 0xff ) ;
		}
		return l ;
	}

	public static String byte2bit (
			byte b
	)
	{
		return "" + ( byte ) ( ( b >> 7 ) & 0x1 ) + ( byte ) ( ( b >> 6 ) & 0x1 ) + ( byte ) ( ( b >> 5 ) & 0x1 ) + ( byte ) ( ( b >> 4 ) & 0x1 ) + ( byte ) ( ( b >> 3 ) & 0x1 ) + ( byte ) ( ( b >> 2 ) & 0x1 )
				+ ( byte ) ( ( b >> 1 ) & 0x1 ) + ( byte ) ( ( b >> 0 ) & 0x1 ) ;
	}

	/**
	 * 打印二进制byte
	 * 
	 */
	public static String bytes2bit (
			byte [ ] source
	)
	{

		StringBuffer sb = new StringBuffer ( ) ;
		for ( byte b : source )
		{
			sb.append ( byte2bit ( b ) ) ;
		}

		return sb.toString ( ) ;

	}

	public static byte [ ] file2bytes (
			File file
	)
	{

		byte [ ] res = new byte [ 0 ] ;
		try
		{
			res = IOUtils.toByteArray ( new FileInputStream ( file ) ) ;
		} catch ( Exception e )
		{
			return null ;
		}

		return res ;

	}

	public static File bytes2file (
			byte [ ] source ,
			String filePath
	)
	{

		File file = new File ( filePath ) ;

		try ( FileOutputStream fos = new FileOutputStream ( file ) )
		{
			fos.write ( source ) ;
		} catch ( Exception e )
		{
			return null ;
		}

		return file ;

	}

	public static void main (
			String [ ] args
	) throws Exception
	{

		byte b = ( byte ) 0xF0 ;
		byte b1 = ( byte ) 0xF5 ;

		byte [ ] ss = new byte [ 2 ] ;
		ss [ 0 ] = b ;
		ss [ 1 ] = b1 ;

		File f = bytes2file ( ss , "d:\\acd.txt" ) ;

		f.createNewFile ( ) ;

	}
}
